package com.ime.elasticsearch.service;

import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service("esService")
@Slf4j
public class ElasticSearchService implements IDefaultElasticSearchService {
    @Autowired(
            required = false
    )
    TransportClient client;

    public ElasticSearchService() {
    }

    public void init() {
        IndicesAdminClient indicesAdminClient = this.client.admin().indices();
        IndicesExistsRequest request = new IndicesExistsRequest();
        request.indices(new String[]{"ime"});

        try {
            boolean exists = ((IndicesExistsResponse)indicesAdminClient.exists(request).actionGet()).isExists();
            if (!exists) {
                AcknowledgedResponse acknowledgedResponse = (AcknowledgedResponse)indicesAdminClient.prepareCreate("ime").setSettings("{\n\"index.mapping.total_fields.limit\": 10000\n}", XContentType.JSON).get();
                log.info("The ES db inited status:" + acknowledgedResponse.isAcknowledged());
            }
        } catch (Exception var5) {
            Exception e = var5;
            log.error("{}", e);
        }

    }

    public String get(String index, String type, String id) {
        GetResponse response = (GetResponse)this.client.prepareGet(index, type, id).get();
        log.info("get " + id + " from es");
        return response.getSourceAsString();
    }

    public void post(String index, String type, String id, Object jsonObject) {
        IndexResponse indexResponse = (IndexResponse)this.client.prepareIndex(index, type, id).setSource(new Object[]{jsonObject, XContentType.JSON}).get();
        log.info("jsonObject索引名称:" + indexResponse.getIndex() + "\n jsonObject类型:" + indexResponse.getType() + "\n jsonObject文档ID:" + indexResponse.getId() + "\n当前实例jsonObject状态:" + indexResponse.status());
    }

    public void post(String index, String type, String id, Map map) {
        IndexResponse indexResponse = (IndexResponse)this.client.prepareIndex(index, type, id).setSource(map).get();
        log.info("jsonObject索引名称:" + indexResponse.getIndex() + "\n jsonObject类型:" + indexResponse.getType() + "\n jsonObject文档ID:" + indexResponse.getId() + "\n当前实例jsonObject状态:" + indexResponse.status());
    }

    public void post(String index, String type, String id, String jsonString) {
        IndexResponse indexResponse = (IndexResponse)this.client.prepareIndex(index, type, id).setSource(jsonString, XContentType.JSON).get();
        log.info("jsonObject索引名称:" + indexResponse.getIndex() + "\n jsonObject类型:" + indexResponse.getType() + "\n jsonObject文档ID:" + indexResponse.getId() + "\n当前实例jsonObject状态:" + indexResponse.status());
    }

    public void delete(String index, String type, String id) {
        DeleteResponse deleteResponse = (DeleteResponse)this.client.prepareDelete(index, type, id).get();
        log.info("deleteResponse索引名称:" + deleteResponse.getIndex() + "\n deleteResponse类型:" + deleteResponse.getType() + "\n deleteResponse文档ID:" + deleteResponse.getId() + "\n当前实例deleteResponse状态:" + deleteResponse.status());
    }

    public void update(String index, String type, String id, Object jsonObject) {
        UpdateResponse updateResponse = (UpdateResponse)this.client.prepareUpdate(index, type, id).setDoc(new Object[]{jsonObject}).get();
        log.info("updateResponse索引名称:" + updateResponse.getIndex() + "\n updateResponse类型:" + updateResponse.getType() + "\n updateResponse文档ID:" + updateResponse.getId() + "\n当前实例updateResponse状态:" + updateResponse.status());
    }

    public void update(String index, String type, String id, Map map) {
        UpdateResponse updateResponse = (UpdateResponse)this.client.prepareUpdate(index, type, id).setDoc(map).get();
        log.info("updateResponse索引名称:" + updateResponse.getIndex() + "\n updateResponse类型:" + updateResponse.getType() + "\n updateResponse文档ID:" + updateResponse.getId() + "\n当前实例updateResponse状态:" + updateResponse.status());
    }

    public void update(String index, String type, String id, String jsonString) {
        UpdateResponse updateResponse = (UpdateResponse)this.client.prepareUpdate(index, type, id).setDoc(new Object[]{jsonString}).get();
        log.info("updateResponse索引名称:" + updateResponse.getIndex() + "\n updateResponse类型:" + updateResponse.getType() + "\n updateResponse文档ID:" + updateResponse.getId() + "\n当前实例updateResponse状态:" + updateResponse.status());
    }
}
